﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OfficialWeb.Application;

using OOS.Application.User.Services.Interfaces;
using OOS.Core.User;

namespace OOS.Application.User.Services
{
    /// <summary>
    /// 用户个人信息管理
    /// </summary>
    public class UserInfoService : IUserInfoService, ITransient
    {
        private readonly ISqlSugarClient _client;

        public UserInfoService(ISqlSugarClient client)
        {
            _client = client;
        }

        /// <summary>
        /// 添加用户个人信息
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public Boolean InsertUserInfo(UserInfo userInfo)
        {
            if (userInfo == null) { return false; }
            return _client.Insertable(userInfo).ExecuteCommand() > 0;
        }
        /// <summary>
        /// 根据用户id获取用户个人信息
        /// </summary>
        /// <returns></returns>
        public UserInfo SelectUserInfoByUserId()
        {
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return new UserInfo(); }
            UserInfo userInfo = _client.Queryable<UserInfo>()
                .Where(ui => ui.UserId == userId)
                .First();
            if (userInfo == null) { return new UserInfo();}
            return userInfo;

        }
        /// <summary>
        /// 根据id修改用户个人信息
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public Boolean UpdateUserInfoById(UserInfo userInfo)
        {
            if (userInfo == null) return false;
            return _client.Updateable(userInfo).ExecuteCommand() > 0;
        }
    }
}
